//
//  YTCarInfoViewController.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/7/7.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTCarInfoViewController.h"
#import "YTCarInfoViewModel.h"
#import "YTCarDetailModel.h"
#import "YTCarNoPassView.h"
#import "YTPopupView.h"
#import "YTCarCertificateViewController.h"

@interface YTCarInfoViewController ()
@property (weak, nonatomic) IBOutlet UILabel *carStates;
@property (weak, nonatomic) IBOutlet UISwitch *carActive;
@property (weak, nonatomic) IBOutlet UILabel *carOwnerText;
@property (weak, nonatomic) IBOutlet UILabel *carInfoText;
@property (weak, nonatomic) IBOutlet UILabel *carNumText;
@property (weak, nonatomic) IBOutlet UILabel *carSeatType;
@property (weak, nonatomic) IBOutlet UILabel *carLevelText;
@property (weak, nonatomic) IBOutlet UILabel *carDateText;
@property (weak, nonatomic) IBOutlet UIButton *carImage1;
@property (weak, nonatomic) IBOutlet UIButton *carImage2;
@property (weak, nonatomic) IBOutlet UIButton *carImage3;
@property (weak, nonatomic) IBOutlet UIImageView *image1;
@property (weak, nonatomic) IBOutlet UIImageView *image2;
@property (weak, nonatomic) IBOutlet UIImageView *image3;
@property (weak, nonatomic) IBOutlet UIButton *submitBtn;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *updateBtn;
@property (strong, nonatomic) YTCarInfoViewModel *viewModel;
@end

@implementation YTCarInfoViewController

- (void)initialize {
    [super initialize];
    _viewModel = [[YTCarInfoViewModel alloc] init];
    _viewModel.car_id = self.car_id;
    // 加载详细信息
    @weakify(self);
    [[_viewModel.loadCarInfo.executionSignals switchToLatest]
     subscribeNext:^(YTCarDetailModel *detailModel) {
         @strongify(self);
         [self loadCarInfoWithModel:detailModel];
    }];
    [_viewModel.loadCarInfo execute:self.car_id];
}

- (void)bindViewModel {
    [super bindViewModel];
    _submitBtn.rac_command = _viewModel.submitCarInfo1;
    // 成功1, 失败0
    @weakify(self);
    [[_viewModel.changeCarState.executionSignals switchToLatest] subscribeNext:^(NSNumber *isSuccess) {
        @strongify(self);
        if (isSuccess.boolValue) {
            NSString *tip = (self.carActive.isOn ? @"启用成功" : @"停用成功");
            [SVProgressHUD showSuccessWithStatus:tip];
        }
    }];
    
    [_viewModel.submitCarInfo1.enabled subscribeNext:^(NSNumber *enable) {
        @strongify(self);
        self.submitBtn.backgroundColor = (enable.boolValue ? kMainBlueColor : [UIColor lightGrayColor]);
    }]; 
    
    [[_viewModel.submitCarInfo1.executionSignals switchToLatest] subscribeNext:^(NSNumber *isSuccess) {
        if (isSuccess.boolValue) {
            [SVProgressHUD showSuccessWithStatus:@"车辆修改成功\n我们将在1-3个工作日内审核完成并告知您审核结果"];
        }
    }];
}

- (void)loadCarInfoWithModel:(YTCarDetailModel *)model {
    _updateBtn.enabled = !(model.applyStatus == 4);
    _carStates.text = model.applyStatusName;
    _carActive.on = model.status;
    _carOwnerText.text = model.carOwner;
    _carInfoText.text = [NSString stringWithFormat:@"%@ %@ %@", model.brandName, model.brandVersionName, model.carColorName];
    _carNumText.text = model.carNumber;
    _carSeatType.text = model.seatTypeName;
    _carLevelText.text = model.carClassName;
    _carDateText.text = model.regTime;
    [_image1 yy_setImageWithURL:[NSURL URLWithString:model.carImage1] placeholder:[UIImage imageNamed:@"car_placehouder"]];
    [_image2 yy_setImageWithURL:[NSURL URLWithString:model.carImage2] placeholder:[UIImage imageNamed:@"car_placehouder"]];
    [_image3 yy_setImageWithURL:[NSURL URLWithString:model.carImage3] placeholder:[UIImage imageNamed:@"car_placehouder"]];
    
    if (model.applyStatus == 3) { // 审核未通过原因
        [self popNoPassTip:model.refuseReason];
    }
}

#pragma mark - 车辆未通过弹窗

- (void)popNoPassTip:(NSString *)reason {
    YTCarNoPassView *noPassView = [[YTCarNoPassView alloc] initWithFrame:self.view.bounds title:reason];
    YTPopupView *popView = [YTPopupView popupWithContentView:noPassView];
    popView.showType = YTPopupShowTypeBounceIn;
    popView.dismissType = YTPopupDismissTypeBounceOut;
    popView.shouldDismissOnContentTouch = YES;
    [popView showWithLayout:YTPopupLayoutMake(YTPopupHorizontalLayoutCenter, YTPopupVerticalLayoutCenter)];
    popView.didFinishDismissingCompletion = ^{
        [self updateAction:self.updateBtn];
    };
}

#pragma mark - Action

- (IBAction)changeCarActiveAction:(UISwitch *)sender {
    NSString *active = sender.isOn ? @"1" : @"0";
    [_viewModel.changeCarState execute:RACTuplePack(self.car_id, active)];
}

- (IBAction)updateAction:(UIBarButtonItem *)sender {
    BOOL update = [sender.title isEqualToString:@"更新"];
    sender.title = (update ? @"取消" : @"更新");
    _carImage1.hidden = !update;
    _carImage2.hidden = !update;
    _carImage3.hidden = !update;
    _submitBtn.hidden = !update;
}

- (IBAction)changeCarImageAction:(UIButton *)sender {
    switch (sender.tag) {
        case 600: {
            @weakify(self);
            [self yt_selecteImageCompletion:^(UIImage * _Nullable image) {
                @strongify(self);
                self.image1.image = image;
                sender.hidden = YES;
                self.viewModel.image1 = image;
            }];
        }
            break;
        case 601:{
            @weakify(self);
            [self yt_selecteImageCompletion:^(UIImage * _Nullable image) {
                @strongify(self);
                self.image2.image = image;
                sender.hidden = YES;
                self.viewModel.image2 = image;
            }];
        }
            break;
        case 602:{
            @weakify(self);
            [self yt_selecteImageCompletion:^(UIImage * _Nullable image) {
                @strongify(self);
                self.image3.image = image;
                sender.hidden = YES;
                self.viewModel.image3 = image;
            }];
        }
            break;
    }
}

#pragma mark - Navigation

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 2 && indexPath.row == 0) {
        if (self.updateBtn.enabled) {
            [self performSegueWithIdentifier:@"YTCarLicenseInfo" sender:nil];
        } else {
            [UIAlertView bk_showAlertViewWithTitle:@"友情提示" message:@"审核或更新中的车辆不能再次更新" cancelButtonTitle:nil otherButtonTitles:@[@"确定"] handler:nil];
        }
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"YTCarLicenseInfo"]) {
        YTCarCertificateViewController *cer = segue.destinationViewController;
        cer.updateViewModel = _viewModel;
        cer.isUpdate = YES;
    }
}

@end
